package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysRoleDTO;
import com.xinsoft.service.SysRoleService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 添加角色
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysRoleDTO sysRoleDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysRoleDTO);
        return Response.succeed(roleService.addSysRole(sysRoleDTO));
    }

    /**
     * 编辑角色
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysRoleDTO sysRoleDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysRoleDTO);
        return Response.succeed(roleService.editSysRole(sysRoleDTO));
    }

    /**
     * 根据id获取角色信息
     */
    @GetMapping("/detail")
    public Response<?> detail(@Valid  @RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(roleService.getSysRole(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@Valid @RequestBody List<Integer> ids) {
        roleService.del(ids);
        return Response.succeed();
    }

    /**
     * 获取角色列表信息
     */
    /**
     * 删除
     */
    @PostMapping("/list")
    public Response<?> list() {
        return Response.succeed(roleService.getRoleList());
    }

}
